package com.focusai.efairy.ui.adapter.msg;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.focusai.efairy.R;
import com.focusai.efairy.model.msg.MsgItem;

import me.drakeet.multitype.ItemViewBinder;

/**
 * Created by Luogw on 2017/8/21.
 */

public class MsgChatLeftViewBinder extends ItemViewBinder<MsgItem, MsgChatLeftViewBinder.ViewHolder> {

    private String deviceName;
    private boolean isSelectType = false;
    private OnItemClickLisenter onItemClickLisenter;

    public MsgChatLeftViewBinder(String deviceName) {
        this.deviceName = deviceName;
    }

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View root = inflater.inflate(R.layout.item_msg_left, parent, false);
        return new ViewHolder(root);
    }

    @Override
    protected void onBindViewHolder(@NonNull final ViewHolder holder, @NonNull final MsgItem item) {
        holder.userName.setText(deviceName);
        holder.msgContent.setText(item.get_content());
        holder.timeText.setText(item.getmsg_time());

        holder.warningBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (item.get_msgtype() == 1) {//需要处理
                    if (onItemClickLisenter != null) {
                        if (item.get_ishandle() == 1) {
                            onItemClickLisenter.onGreenBtnClick(item);
                        } else if(!isSelectType){
                            onItemClickLisenter.onRedBtnClick(item);
                        }else{
                            if(item.isSelected){
                                holder.ivselected.setChecked(false);
                                item.isSelected = false;
                            }else{
                                holder.ivselected.setChecked(true);
                                item.isSelected = true;
                            }
                            return;
                        }
                    }
                }
            }
        });

//        holder.rl_view.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                if(isSelectType){
//                    if(holder.ivselected.isChecked()){
//                        holder.ivselected.setChecked(false);
//                        onItemClickLisenter.OnCheckedChange(item,false);
//                    }else{
//                        holder.ivselected.setChecked(true);
//                        onItemClickLisenter.OnCheckedChange(item,true);
//                    }
//                }
//            }
//        });
//        holder.itemView.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                if (onItemClickLisenter != null && isSelectType && item.get_ishandle() != 1) {
//                    item.isSelected = !item.isSelected;
//                    onItemClickLisenter.onItemClick(item);
//                }
//            }
//        });



        holder.ivselected.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                item.isSelected = isChecked;
                if(onItemClickLisenter!=null){
                    onItemClickLisenter.OnCheckedChange(item,isChecked);
                }
            }
        });


        holder.warningBtn.setVisibility(View.GONE);
        holder.ivselected.setVisibility(View.GONE);
        holder.ivselected.setChecked(item.isSelected);
        holder.itemView.setBackgroundResource(R.color.bg_gray);
        if (onItemClickLisenter != null) {
            holder.avater.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onItemClickLisenter.onAvaterBtnClick(item);
                }
            });
        }
        if (item.get_msgtype() == 1) {//需要处理
            holder.warningBtn.setVisibility(View.VISIBLE);
//            holder.warningBtn.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    if (onItemClickLisenter != null) {
//                        if (item.get_ishandle() == 1) {
//                            onItemClickLisenter.onGreenBtnClick(item);
//                        } else {
//                            onItemClickLisenter.onRedBtnClick(item);
//                        }
//                    }
//                }
//            });
            if (item.get_ishandle() == 1) {  //已处理
                holder.warningBtn.setImageResource(R.drawable.warnning_green_icon);
            } else { //还未处理
                holder.warningBtn.setImageResource(R.drawable.warning_red_icon);
                if (isSelectType) { //进入选择模式
                    holder.ivselected.setVisibility(View.VISIBLE);
//                    if (item.isSelected) {
                        holder.ivselected.setChecked(item.isSelected);
//                        holder.itemView.setBackgroundResource(R.color.backgroud_selected);
//                    } else {
//                        holder.ivselected.setChecked(false);
//                        holder.itemView.setBackgroundResource(R.color.bg_gray);
//                    }
//                    if (onItemClickLisenter != null) {
//                        holder.itemView.setOnClickListener(new View.OnClickListener() {
//                            @Override
//                            public void onClick(View v) {
//                                item.isSelected = !item.isSelected;
//                                onItemClickLisenter.onItemClick(item);
//                            }
//                        });
//                    } else {
//                        holder.itemView.setOnClickListener(null);
//                    }
                }
            }
        }
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView avater;
        CheckBox ivselected;
        TextView userName;
        TextView msgContent;
        TextView timeText;
        ImageView warningBtn;
        RelativeLayout rl_view;

        ViewHolder(View itemView) {
            super(itemView);
            rl_view = (RelativeLayout)itemView.findViewById(R.id.rl_view);
            avater = (ImageView) itemView.findViewById(R.id.msg_user_avater);
            ivselected = (CheckBox) itemView.findViewById(R.id.iv_selected);
            userName = (TextView) itemView.findViewById(R.id.chat_msg_name);
            msgContent = (TextView) itemView.findViewById(R.id.chat_msg_content);
            timeText = (TextView) itemView.findViewById(R.id.chatting_msg_item_tv_sendtime);
            warningBtn = (ImageView) itemView.findViewById(R.id.iv_btn_warning);
        }
    }


    public void setSelectType(boolean selectType) {
        isSelectType = selectType;
    }

    public void setOnItemClickLisenter(OnItemClickLisenter onItemClickLisenter) {
        this.onItemClickLisenter = onItemClickLisenter;
    }

    public interface OnItemClickLisenter {
        void onItemClick(MsgItem item);

        void onRedBtnClick(MsgItem item);

        void onGreenBtnClick(MsgItem item);

        void onAvaterBtnClick(MsgItem item);

        void OnCheckedChange(MsgItem item,boolean isChecked);
    }


}
